package com.cn.wanxi.servlet.product;

import com.alibaba.fastjson.JSONObject;
import com.cn.wanxi.model.ProductModel;
import com.cn.wanxi.service.IProductService;
import com.cn.wanxi.service.impl.ProductServiceImpl;
import com.cn.wanxi.util.IOTool;
import com.cn.wanxi.util.LoadImg;
import com.cn.wanxi.util.MyPath;
import com.cn.wanxi.util.Tool;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

@WebServlet("/backFile/product/add")
public class ProductAddServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        1.乱码问题
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
//        2.取得前台传递的值
//        找到当前用户是否已经登录，如果没有登录，则直接返回登录页面
//req在文件流的时候只能拿到一次，所以页不能进入过滤器,
//        如果没有登录，则直接返回jsonObject.put("result", "noLogin");
//        否则进入else
        HttpSession session = req.getSession();
        String key = req.getParameter("loginUsername");
        String username = (String) session.getAttribute(key); // 在登录时将 User 对象放入了 会话
        JSONObject jsonObject = new JSONObject();
        String result = "-1";
        if (username == null) {
            jsonObject.put("result", "noLogin");
        } else {
            LoadImg.loadImg(req);
            Object nameObj = req.getAttribute("name");
            String name = (String) nameObj;
            Object priceObj = req.getAttribute("price");
            String price = (String) priceObj;
//        String normalPrice = req.getParameter("normalPrice");
//        String imgHref = req.getParameter("imgHref");
//        String cuisineId = req.getParameter("cuisineId");
//        String isShow = req.getParameter("isShow");
//        String content = req.getParameter("content");

            //        //        3.封装数据
            ProductModel productModel = new ProductModel();
            productModel.setName(name);
            productModel.setPrice(Tool.stringToDouble(price));
            productModel.setNormalPrice(Tool.stringToDouble((String) req.getAttribute("normalPrice")));
            productModel.setImgHref((String) req.getAttribute("imgHref"));
            productModel.setCuisineId(Tool.stringToInt((String) req.getAttribute("cuisineId")));
            productModel.setIsShow(Tool.stringToInt((String) req.getAttribute("isShow")));
            productModel.setContent((String) req.getAttribute("content"));
//        4.调用服务逻辑层
            IProductService iProductService = new ProductServiceImpl();
//        5.得到数据
            result = iProductService.add(productModel);

        }

//        6.返回结果
        jsonObject.put("result", result);
        resp.getWriter().println(jsonObject);
    }
}

